﻿using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Model.Shipping;
using System.Collections.Generic;
using SIG.Resources;
using SIG.Infrastructure.Configs;
using System.Linq;
using SIG.Resources.Admin;
using SIG.Infrastructure.Helper;
using System;

namespace SIG.Service
{
    public class RecipientInfoService :  EntityService<RecipientInfo>, IRecipientInfoService
    {
       
        private readonly IRecipientInfoRepository _recipientInfoRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public RecipientInfoService(IRecipientInfoRepository recipientInfoRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(recipientInfoRepository, cacheService, loggingService)
        {           
            _recipientInfoRepository = recipientInfoRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public RecipientInfo GetRecipientInfoDetail(int id)
        {
            return _recipientInfoRepository.GetFirstOrDefault(m => m.Id == id, m => m.Province,m=>m.City,m=>m.District);
        }
        public List<RecipientInfo> GetRecipientInfoes(string username)
        {

            var key = $"{EntityNames.RecipientInfo}s_{username}";

            if (!SettingsManager.Store.EnableCaching)
            {
                return _recipientInfoRepository.GetFilteredElements( m => m.CreatedBy == username, m=>m.Province,m=>m.City, m=>m.District).ToList();
            }

            if (_cacheService.IsSet(key))
                return (List<RecipientInfo>)_cacheService.Get(key);

            List<RecipientInfo> recipientInfoes = _recipientInfoRepository.GetFilteredElements(m => m.CreatedBy == username, m => m.Province, m => m.City, m => m.District).ToList(); ;
            _cacheService.Set(key, recipientInfoes, SettingsManager.Article.CacheDuration);

            return recipientInfoes;
        }


        public RecipientInfo SetDefault(int id)
        {
            try
            {
                var rInfo = _recipientInfoRepository.GetElementById(id);
                if (rInfo == null)
                {
                    return null;
                }

                if (!rInfo.IsDefault)
                {
                    var accounts = _recipientInfoRepository.GetFilteredElements(m => m.IsDefault && m.Id != id);
                    foreach (var item in accounts)
                    {
                        item.IsDefault = false;
                        _recipientInfoRepository.SetModified(item);
                    }
                    rInfo.IsDefault = true;
                }
                else
                {
                    rInfo.IsDefault = false;
                }
                _recipientInfoRepository.SetModified(rInfo);
                _recipientInfoRepository.UnitOfWork.Commit();

                _cacheService.Invalidate(EntityNames.RecipientInfo);
                _loggingService.Info(string.Format(Logs.SetDefaultEmailAccount, Site.CurrentUserName, rInfo.Recipient));
                return rInfo;
            }
            catch (Exception er)
            {
                _loggingService.Error(er.Message, er);
                return null;
            }

        }

    }
}
